package org.aplikator.server.query;

import org.aplikator.client.shared.descriptor.QueryParameter;
import org.aplikator.server.descriptor.Property;

import java.io.Serializable;
import java.util.List;

public class QueryCompareExpression<T extends Serializable> extends QueryExpression {

    private final Property<T> property;
    private final QueryCompareOperator operator;
    private T value;
    private final QueryParameterReference parameterReference;

    public QueryCompareExpression(Property<T> property, QueryCompareOperator operator, T value) {
        super();
        this.property = property;
        this.operator = operator;
        this.value = value;
        this.parameterReference = null;
    }

    public QueryCompareExpression(Property<T> property, QueryCompareOperator operator, QueryParameterReference parameterReference) {
        super();
        this.property = property;
        this.operator = operator;
        this.value = null;
        this.parameterReference = parameterReference;
    }

    public Property<T> getProperty() {
        return property;
    }

    public QueryCompareOperator getOperator() {
        return operator;
    }

    public T getValue() {
        return value;
    }

    @SuppressWarnings({ "unchecked" })
    public void bindParameters(List<QueryParameter> parameters){
        if (parameterReference != null) {
            value = (T) parameters.get(parameterReference.getParameterIndex()).getValue();
        }
    }

}
